package com.example.myprojects;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myprojects.uitl.MyListBean;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;

import java.util.List;

public class MyListApapter extends BaseAdapter {
    List<MyNews.ResultBean.DataBean> list;
    Context context;

    public MyListApapter(List<MyNews.ResultBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list!=null?list.size():0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    public int getViewTypeCount() {
        return 2;
    }
    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    static class ViewHedle1{
        ImageView imageView;
        TextView tv1;
        TextView tv2;
        TextView tv3;
    }
    static  class  ViewHedle2{
        ImageView img1,img2,img3;
        TextView tv_title,tv_author,tv_time;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHedle1 vh1;
        ViewHedle2 vh2;
        vh1=new ViewHedle1();
        vh2=new ViewHedle2();
        int type=getItemViewType(position);
        Picasso picasso=Picasso.with(context);
        MyNews.ResultBean.DataBean bean=list.get(position);
        if (convertView==null){
            switch (type){
                case 0:
                    convertView= LayoutInflater.from(context).inflate(R.layout.list_items,null);
                    vh1.tv1=convertView.findViewById(R.id.tv1);
                    vh1.tv2=convertView.findViewById(R.id.tv2);
                    vh1.tv3=convertView.findViewById(R.id.tv3);
                    vh1.imageView=convertView.findViewById(R.id.imgView);
                    vh1.tv1.setText(bean.getTitle());
                    vh1.tv2.setText(bean.getAuthor_name());
                    vh1.tv3.setText(bean.getDate());
                    Log.e("", "getView: "+bean.getTitle() );
                    RequestCreator load = picasso.load(bean.getThumbnail_pic_s());
                    load.into(vh1.imageView);
                    convertView.setTag(vh1);
                    break;
                case 1:
                    convertView= LayoutInflater.from(context).inflate(R.layout.list_items2,null);
                    vh2.tv_title=convertView.findViewById(R.id.tv_title);
                    vh2.tv_author=convertView.findViewById(R.id.tv_author);
                    vh2.tv_time=convertView.findViewById(R.id.tv_time);
                    vh2.img1=convertView.findViewById(R.id.img1);
                    vh2.img2=convertView.findViewById(R.id.img2);
                    vh2.img3=convertView.findViewById(R.id.img3);
                    vh2.tv_title.setText(bean.getTitle());
                    vh2.tv_author.setText(bean.getAuthor_name());
                    vh2.tv_time.setText(bean.getDate());
                    RequestCreator load1 = picasso.load(bean.getThumbnail_pic_s());
                    load1.into(vh2.img1);
                    RequestCreator load2 = picasso.load(bean.getThumbnail_pic_s02());
                    load2.into(vh2.img2);
                    RequestCreator load3 = picasso.load(bean.getThumbnail_pic_s03());
                    load3.into(vh2.img3);
                    convertView.setTag(vh2);
                    break;
            }
            Log.e("", "getView: "+bean.getTitle() );
        }else{
            switch (type){
                case 0:
                    vh1= (ViewHedle1) convertView.getTag();
                    break;
                case 1:
                    vh2= (ViewHedle2) convertView.getTag();
            }
        }
        switch (type){
            case 0:
                vh1.tv1.setText(bean.getTitle());
                vh1.tv2.setText(bean.getAuthor_name());
                vh1.tv3.setText(bean.getDate());
                Log.e("", "getView: "+bean.getTitle() );
                RequestCreator load = picasso.load(bean.getThumbnail_pic_s());
                load.into(vh1.imageView);
                break;
            case 1:
                vh2.img1=convertView.findViewById(R.id.img1);
                vh2.img2=convertView.findViewById(R.id.img2);
                vh2.img3=convertView.findViewById(R.id.img3);
                vh2.tv_title.setText(bean.getTitle());
                vh2.tv_author.setText(bean.getAuthor_name());
                vh2.tv_time.setText(bean.getDate());
                RequestCreator load1 = picasso.load(bean.getThumbnail_pic_s());
                load1.into(vh2.img1);
                RequestCreator load2 = picasso.load(bean.getThumbnail_pic_s02());
                load2.into(vh2.img2);
                RequestCreator load3 = picasso.load(bean.getThumbnail_pic_s03());
                load3.into(vh2.img3);
                break;
        }
        return convertView;
    }
}
